package com.itqf.quartz;

import com.itqf.entity.ScheduleJob;
import com.itqf.entity.ScheduleJobLog;
import com.itqf.utils.SpringUtils;
import com.itqf.utils.SysConstant;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.lang.reflect.Method;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/27
 * @Time: 下午4:15
 */
public class MyQuartzJob implements Job {

   // @Resource  因为MyQuartzJob 不属于Spring容器，所以不能注入Spring容器中的对象
    //private BackupDb backupDb;//注入不了。。。。

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //System.out.println("helloworld！！！"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
//        BackupDb backupDb = new BackupDb();
//        backupDb.backUpDb();
//
//        OrderStatusTask task = new OrderStatusTask();
//        task.changeOrderStatus();

        try {
            ScheduleJob scheduleJob =  (ScheduleJob) jobExecutionContext.getJobDetail().getJobDataMap().get(SysConstant.TASK_DATA);
            String  beanName = scheduleJob.getBeanName();//backUpDb
            String methodName = scheduleJob.getMethodName();//backUp
            String params = scheduleJob.getParams();
// ApplicationContext ac = new ClassPathXmlApplicationContext("axxx.xml")
//        ac.getBean(beanName);
            Object obj = SpringUtils.getBean(beanName);
            //已知对象，方法名，如何调用该方法？？？
           if(params==null){
                Method method =  obj.getClass().getMethod(methodName);
                method.invoke(obj,null);//无参方法
           }else{
               Method method =  obj.getClass().getMethod(methodName,String.class);
               method.invoke(obj,params);//带参方法
           }

           //记录一波日志
            ScheduleJobLog scheduleJobLog =  new ScheduleJobLog();
           //scheduleJobLog.
            //参照日志记录？
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
